/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.UI;

import com.usoog.tdcore.message.GameInfo;
import java.awt.Component;
import java.util.WeakHashMap;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class ChannelListRenderer implements ListCellRenderer {

	private static WeakHashMap<GameInfo, ChannelListPanel> panelCache = new WeakHashMap<GameInfo, ChannelListPanel>();

	public ChannelListRenderer() {
	}
	// This is the only method defined by ListCellRenderer.
	// We just reconfigure the JLabel each time we're called.

	public Component getListCellRendererComponent(
			JList list, // the list
			Object value, // value to display
			int index, // cell index
			boolean isSelected, // is the cell selected
			boolean cellHasFocus) // does the cell have focus
	{
		if (value instanceof GameInfo) {
			GameInfo info = (GameInfo) value;
			ChannelListPanel panel = panelCache.get(info);
			if (panel == null) {
				panel = new ChannelListPanel(info);
				panelCache.put(info, panel);
			}
			panel.setSelected(isSelected);
			return panel;
		}
		return new JLabel("Unknown item");
	}
}
